package duyd.jpa.training.service.jpa;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import duyd.jpa.training.domain.MyEntity;
import duyd.jpa.training.service.MyEntityService;

@Repository
@Service("myEntityService")
@Transactional
public class MyEntityServiceImpl implements MyEntityService {

	@PersistenceContext
	EntityManager em;

	public List<MyEntity> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MyEntity> findBeforeCreateDate(Date createDate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly = true)
	public List<MyEntity> findAllWithDetails() {
		return em
				.createNamedQuery("myEntity.findAllWithDetail", MyEntity.class)
				.getResultList();
	}

	public void batchSave(List<MyEntity> myEntityList, int batchSize) {
		int n = myEntityList.size();
		for (int i = 0; i < n; i++) {
			MyEntity myEntity = myEntityList.get(i);
			if (myEntity.getId() != null) {
				em.merge(myEntity);
			} else {
				em.persist(myEntity);
			}

			if (i % batchSize == 0) {
				em.flush();
				em.clear();
			}
		}
	}

	public void batchUpdate(List<MyEntity> myEntityList, int batchSize) {
		// TODO Auto-generated method stub

	}

	public void batchDelete(List<MyEntity> myEntityList, int batchSize) {
		// TODO Auto-generated method stub

	}

}
